<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/8/24
 * Time: 18:12
 */

namespace ticket\modules\frontend\controllers;


use ticket\libs\Tools;
use ticket\modules\frontend\models\TicketCategory;
use ticket\modules\frontend\models\TicketInfo;
use ticket\modules\frontend\models\TicketType;
use ticket\modules\frontend\models\OrderInfo;
use Yii;
use yii\helpers\ArrayHelper;
use yii\base\Exception;
use yii\web\Controller;

class OutController extends Controller
{

    /**** 获取分时段列表
     */
    public function actionDayPartList()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $app_id = isset($post['appId'])?$post['appId']:'';
            if  (empty($app_id)){
                return Tools::showRes(10404,'参数有误');
            }

            $channelId = $post['channelId'];
            $playDate = isset($post['playDate']) ? $post['playDate'] : date("Y-m-d");

            $url = 'http://121.236.175.40:8081/SelfHelpSystem/inteface/day/getDaypartingList.json';

            $post_data['channelId'] = $channelId;
            $post_data['playDate'] = $playDate;

            $postdata = http_build_query($post_data);
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'header' => 'Content-type:application/x-www-form-urlencoded',
                    'content' => $postdata,
                    'timeout' => 15 * 60
                )
            );
            $context = stream_context_create($options);
            $result = file_get_contents($url, false, $context);

            $res = json_decode($result, true);

            // var_dump($result);
    
            return Tools::showRes(0,'success',[
                'data' => $res['data'],
            ]);

        }
    }


    /**** 预约扣库存
     */
    public function actionCutStock()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $app_id = isset($post['appId'])?$post['appId']:'';
            if  (empty($app_id)){
                return Tools::showRes(10404,'参数有误');
            }

            $channelId = $post['channelId'];
            $id = $post['id'];
            $name = $post['name'];
            $occDate = $post['occDate'];
            $num = $post['num'];
            $phone = $post['phone'];
            $type = $post['type'];

            $url = 'http://121.236.175.40:8081/SelfHelpSystem/inteface/day/cutStock.json';

            $post_data['channelId'] = $channelId;
            $post_data['id'] = $id;
            $post_data['name'] = $name;
            $post_data['occDate'] = $occDate;
            $post_data['num'] = $num;
            $post_data['phone'] = $phone;
            $post_data['type'] = $type;

            $postdata = http_build_query($post_data);
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'header' => 'Content-type:application/x-www-form-urlencoded',
                    'content' => $postdata,
                    'timeout' => 15 * 60
                )
            );
            $context = stream_context_create($options);
            $result = file_get_contents($url, false, $context);

            $res = json_decode($result, true);

            if(isset($res['code']) && $res['code'] == '00'){
                return Tools::showRes(0, 'success', ['code'=>0, 'message'=>'记录成功']);
            }else{
                return Tools::showRes(0,'success',$res);
            }
    
            

        }
    }


    /**** 全部订单
     */
    public function actionAllOrder()
    {


        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
          

            $openid = $post['openid'];

            $page = 1;
            $size = 100;
            $limit = 100;


            //未支付餐饮
            $url = 'https://jqxcx.klygf.com/CaterSystem/inteface/v2/order/searchOrders.json';

            $post_data['openId'] = $openid;
            $post_data['page'] = $page;
            $post_data['status'] = 0;


            $postdata = http_build_query($post_data);
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'header' => 'Content-type:application/x-www-form-urlencoded',
                    'content' => $postdata,
                    'timeout' => 15 * 60
                )
            );
            $context = stream_context_create($options);
            $result = file_get_contents($url, false, $context);

            $res = json_decode($result, true);

            if(isset($res['code']) && $res['code'] == '00'){
                $eat1 = $res['data'];
                foreach($eat1  as &$val){
                    $val['cate'] = 1;
                    $val['order_time'] = $val['billdate'];
                }
            }else{
                $eat1 = [];
            }
            //未支付餐饮

            

            //已支付餐饮
            $url = 'https://jqxcx.klygf.com/CaterSystem/inteface/v2/order/searchOrders.json';

            $post_data['openId'] = $openid;
            $post_data['page'] = $page;
            $post_data['status'] = 1;


            $postdata = http_build_query($post_data);
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'header' => 'Content-type:application/x-www-form-urlencoded',
                    'content' => $postdata,
                    'timeout' => 15 * 60
                )
            );
            $context = stream_context_create($options);
            $result = file_get_contents($url, false, $context);

            $res = json_decode($result, true);

            if(isset($res['code']) && $res['code'] == '00'){
                $eat2 = $res['data'];
                foreach($eat2  as &$val){
                    $val['cate'] = 1;
                    $val['order_time'] = $val['billdate'];
                }
            }else{
                $eat2 = [];
            }
            //已支付餐饮


            //门票
            $url = 'https://ticket.raysfly.com/frontend/ticket-order/user-ticket-list';

            $post_data['openid'] = $openid;
            $post_data['page'] = $page;
            $post_data['size'] = $size;


            $postdata = http_build_query($post_data);
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'header' => 'Content-type:application/x-www-form-urlencoded',
                    'content' => $postdata,
                    'timeout' => 15 * 60
                )
            );
            $context = stream_context_create($options);
            $result = file_get_contents($url, false, $context);

            $res = json_decode($result, true);

            if(isset($res['code']) && $res['code'] == '0'){
                $ticket = $res['data']['orderList'];
                foreach($ticket  as $key=>&$val){
                    if($val['order_status'] == 2){
                        unset($ticket[$key]);
                        continue;
                    }
                    $val['cate'] = 2;
                    $val['order_time'] = $val['create_time'];
                }
            }else{
                $ticket = [];
            }



            //门票



            //酒店

            $headers = Yii::$app->getRequest()->getHeaders();
            $Authorization = $headers->get('Authorization');
            $cookie = $headers->get('cookie');

            $url = 'https://hotel.raysfly.com/api/hotel/order-list';
            $post_data['page'] = $page;
            $post_data['limit'] = $limit;
            $postdata = http_build_query($post_data);
            $header = ["Content-type:application/json;chartset=UTF-8", "Authorization:$Authorization" , "cookie:$cookie" ] ;
            
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'header' => $header,
                    'content' => $postdata,
                    'timeout' => 15 * 60
                )
            );
            $context = stream_context_create($options);
            $result = file_get_contents($url, false, $context);

            $res = json_decode($result, true);

            if(isset($res['code']) && $res['code'] == '0'){
                $hotel = $res['data']['list'];
                foreach($hotel  as &$val){
                    $val['cate'] = 3;
                    $val['order_time'] = date('Y-m-d H:i:s', $val['create_time']);
                }
            }else{
                $hotel = [];
            }


            //酒店


            //商品

            $headers = Yii::$app->getRequest()->getHeaders();
            $Authorization = $headers->get('Authorization');
            $cookie = $headers->get('cookie');


            $url = 'https://shop.raysfly.com/api/order/order-list';
            $post_data['page'] = $page;
            $post_data['limit'] = $limit;
            $postdata = http_build_query($post_data);
            $header = ["Content-type:application/json;chartset=UTF-8", "Authorization:$Authorization" , "cookie:$cookie" ] ;
            
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'header' => $header,
                    'content' => $postdata,
                    'timeout' => 15 * 60
                )
            );
            $context = stream_context_create($options);
            $result = file_get_contents($url, false, $context);

            $res = json_decode($result, true);

            if(isset($res['code']) && $res['code'] == '0'){
                $goods = $res['data']['list'];
                foreach($goods  as &$val){
                    $val['cate'] = 4;
                    $val['order_time'] = date('Y-m-d H:i:s', $val['create_time']);
                }
            }else{
                $goods = [];
            }


            //商品


            //合并
            $meres = array_merge($eat1,$eat2,$ticket,$hotel,$goods);

            //排序
            ArrayHelper::multisort($meres, ['order_time'], [SORT_DESC]);


            return Tools::showRes(0,'success',$meres);

            // return Tools::showRes(0,'success',$res);
            

            // if(isset($res['code']) && $res['code'] == '00'){
            //     return Tools::showRes(0, 'success', ['code'=>0, 'message'=>'记录成功']);
            // }else{
            //     return Tools::showRes(0,'success',$res);
            // }
    
            

        }
    }
 

}